package xml_study;


import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

import java.util.Iterator;

public class MyDom4j {

    public  void test(){
        //创建解析器
        SAXReader saxReader = new SAXReader();
        try {
            Document document = saxReader.read(this.getClass().getClassLoader().getResource("students.xml").getPath());
            //获取根节点
            Element rootElement = document.getRootElement();
            //遍历根节点
            Iterator<Element> rootIterator = rootElement.elementIterator();

            Node node = document.selectSingleNode("/students/student/college");//XPath路径表达式（符合Xpath规则），需要导入“jaxen”jar包
            String stringValue1 = node.getStringValue();
            System.out.println(stringValue1);
            while(rootIterator.hasNext()){
                Element element = rootIterator.next();
                String name = element.elementText("name");
                System.out.println(name);
                Iterator<Element> elementIterator = element.elementIterator();
                while(elementIterator.hasNext()){
                    Element element1 = elementIterator.next();
                    String stringValue = element1.getStringValue();
                    String name1 = element1.attributeValue("nic");
                    System.out.println(name1);
                }

            }

        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
       new MyDom4j().test();
    }
}
